//
//  UrlCreator.m
//  RunimgService
//
//  Created by lizq on 16/4/20.
//  Copyright © 2016年 lizq. All rights reserved.
//

#import "UrlCreator.h"
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonHMAC.h>


@interface UrlCreator ()
@property(nonatomic,strong)NSString *tokenId;
@property(nonatomic,strong)NSString *tokenKey;
@property(nonatomic,assign)ImageType imageType;
@property(nonatomic,assign)NSInteger expired;
@property(nonatomic,strong)ImageOperator *imageOperator;
@property(nonatomic,strong)NSString *timestamp;
@end

@implementation UrlCreator

/*
 * @tokenId   用户tokenId
 * @tokenKey  用户tokenKey
 * @imageType 原图片大小 @ImageType
 * @expired   目标图片的有效时间
 * @imageOperator 图片的处理对象
 * @return    请求图片的对象
 */
- (instancetype)initWithTokenId:(NSString *)tokenId
                       tokenKey:(NSString *)tokenKey
                      imageType:(ImageType )imageType
                        expired:(NSInteger )expired
                  imageOperator:(ImageOperator *)imageOperator {
    self = [super init];
    if(self){
        self.tokenId = tokenId;
        self.tokenKey = tokenKey;
        self.imageType = imageType;
        self.expired = expired;
        self.imageOperator = imageOperator;
        self.timestamp = [NSString stringWithFormat:@"%.0f",[[NSDate date] timeIntervalSince1970]];
    }
    return self;
}


/*
 * 签名获得请求的URL地址
 */
- (NSString *)toUrlString {
    NSMutableString *content = [[NSMutableString alloc] init];
    [content appendFormat:@"%@=%ld",@"expired",_expired];
    [content appendFormat:@"&%@=%@",@"img_opt",[[_imageOperator toString] urlEncode]];
    [content appendFormat:@"&%@=%@",@"img_type",[[self conversionImageType] urlEncode]];
    [content appendFormat:@"&%@=%@",@"timestamp",[_timestamp urlEncode]];
    [content appendFormat:@"&%@=%@",@"token_id", [_tokenId urlEncode]];
    [content appendFormat:@"&%@=%@",@"version",[@"1.0" urlEncode]];
    [content appendFormat:@"&%@=%@",@"signature",[[self signature] urlEncode]];
     NSString *url = [NSString stringWithFormat:@"%@%@",BASE_URL,content];
    return url;
}

- (NSString *)signature {
    NSMutableString *content = [[NSMutableString alloc] init];
    [content appendFormat:@"%@=%ld",@"expired",_expired];
    [content appendFormat:@"&%@=%@",@"img_opt",[_imageOperator toString]];
    [content appendFormat:@"&%@=%@",@"img_type",[self conversionImageType]];
    [content appendFormat:@"&%@=%@",@"timestamp",_timestamp];
    [content appendFormat:@"&%@=%@",@"token_id",_tokenId];
    [content appendFormat:@"&%@=%@",@"version",@"1.0"];
    NSString *signature = [content HMAC_SHA1WithKey:_tokenKey];
    return signature;
}

- (NSString *)conversionImageType {
    //1d_0_0 模式
    NSString *type = @"";
    switch (self.imageType) {
        case TYPE_1D:
            type = @"1d";
            break;
        case TYPE_2D:
            type = @"2d";
            break;
        case TYPE_2D_0_0:
            type = @"2d_0_0";
            break;
        case TYPE_2D_0_1:
            type = @"2d_0_1";
            break;
        case TYPE_2D_1_0:
            type = @"2d_1_0";
            break;
        case TYPE_2D_1_1:
            type = @"2d_1_1";
            break;
        case TYPE_4D:
            type = @"4d";
            break;
        case TYPE_4D_0_0:
            type = @"4d_0_0";
            break;
        case TYPE_4D_0_1:
            type = @"4d_0_1";
            break;
        case TYPE_4D_0_2:
            type = @"4d_0_2";
            break;
        case TYPE_4D_0_3:
            type = @"4d_0_3";
            break;
        case TYPE_4D_1_0:
            type = @"4d_1_0";
            break;
        case TYPE_4D_1_1:
            type = @"4d_1_1";
            break;
        case TYPE_4D_1_2:
            type = @"4d_1_2";
            break;
        case TYPE_4D_1_3:
            type = @"4d_1_3";
            break;
        case TYPE_4D_2_0:
            type = @"4d_2_0";
            break;
        case TYPE_4D_2_1:
            type = @"4d_2_1";
            break;
        case TYPE_4D_2_2:
            type = @"4d_2_2";
            break;
        case TYPE_4D_2_3:
            type = @"4d_2_3";
            break;
        case TYPE_4D_3_0:
            type = @"4d_3_0";
            break;
        case TYPE_4D_3_1:
            type = @"4d_3_1";
            break;
        case TYPE_4D_3_2:
            type = @"4d_3_2";
            break;
        case TYPE_4D_3_3:
            type = @"4d_3_3";
            break;
        default:
            type = @"2d";
            break;
    }
    return type;
}

@end


#pragma mark ---
#pragma mark NSString+Runimg
@implementation NSString (Runimg)

/*
 * url 编码
 * URL编码后的字符串
 */
- (NSString *)urlEncode {
    NSString * encodedString = [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return encodedString;
}

/*
 * url 解码
 * @return URL解码的字符串
 */
- (NSString *)urlDecode {
    NSString *decodedString = [self stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return decodedString;
}

/*
 * HMAC SHA1 加密
 * @key 加密的密钥
 * @return 加密后的字符串
 */
- (NSString *)HMAC_SHA1WithKey:(NSString *)key {
    const char *cKey   = [key cStringUsingEncoding:NSUTF8StringEncoding];
    const char *cData  = [self cStringUsingEncoding:NSUTF8StringEncoding];
    unsigned char cHMAC[CC_SHA1_DIGEST_LENGTH];
    CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
    NSData *HMAC   = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
    NSString *hash = [HMAC base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return hash;
}

@end
